﻿using System;
using System.Collections;
using System.Collections.Generic;
using TrueSync;
using UnityEngine;
using GameProtocols;
using Framework;

public class LogicMoveComponent : LogicComponent
{
    LogicEntity mLEntity;
    TSVector2 mPosition;
    int mFaceDirection;

    public void Destroy()
    {
    }

    public void DoUpdate(FP deltaTime)
    {
        //face other
        FightGame game = (FightGame)GameManager.Instance.Game;
        ObjHandle<LogicEntity> mRival = game.GetRival(mLEntity);
        if (mRival.IsValid)
            FaceOther(mRival.handle.GetLogicComponent<LogicMoveComponent>());
    }

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;
        mFaceDirection = 0;
    }

    public void Start()
    {
        
    }

    public void SetPosition(TSVector2 position)
    {
        mPosition = position;
        mLEntity.SendEntityMessage((int)ProtoId.MSG_SETPOSITION, mPosition);
    }
    

    void FaceOther(LogicMoveComponent otherPlayer)
    {
        int direction = otherPlayer.mPosition.x > mPosition.x ? 1 : -1;
        if(direction != mFaceDirection)
        {
            mFaceDirection = direction;
            mLEntity.SendEntityMessage((int)ProtoId.MSG_SETROTATION, mFaceDirection);
        }
        
    }
}
